package com.shuqi.model.manager;

import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.shuqi.common.Config;
import com.shuqi.common.LoadingImg;
import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.SharedPreferenceUtil;
import com.shuqi.common.utils.ToastUtil;
import com.shuqi.model.GetSoftConfigOnlineModel;
import com.shuqi.model.GetSoftConfigOnlineModel.OnGetSoftConfigInfoListener;
import com.shuqi.model.bean.OfferWallAppInfo;
import com.shuqi.model.bean.SoftConfigInfo;
import com.shuqi.model.bean.SoftUpdateInfo;
import com.shuqi.model.sharedprefs.ServerDomains;
import com.shuqi.model.sharedprefs.StaticDomains;
import com.shuqi.model.sharedprefs.WebDomains;
import com.shuqi.model.sharedprefs.WebUrls;

public class SoftCofigManager {
    private static SoftCofigManager cofigManager;
    private static String logTag = "SoftCofigManager";
    private static SoftConfigInfo softConfigInfo;

    private SoftCofigManager() {}

    public static SoftCofigManager getInstance() {
        if (cofigManager == null) {
            cofigManager = new SoftCofigManager();
        }
        return cofigManager;
    }

    /***
     * 获取更新数据
     * 
     * @param context
     * @return
     */
    public SoftUpdateInfo getUpdateInfo() {
        Log4an.e(logTag, "等待获取更新数据..");
        synchronized (SoftCofigManager.class) {
            Log4an.e(logTag, "获取更新数据...");
            if (softConfigInfo != null) {
                return softConfigInfo.getUpdateInfo();
            }
            return null;
        }
    }

    /***
     * 加载AppInfo接口获取软件基本信息
     * 
     * @param context 全局的ApplicationContext
     */
    public void loadSoftConfigInfo(final Context context) {
        OnGetSoftConfigInfoListener listener = new OnGetSoftConfigInfoListener() {
            @Override
            public void OnFinish(int type, final SoftConfigInfo info) {
                if (type == 1) {
                    //手机时间错误，提示调整时间
                    if(info.getServerTime()> 0 && !TextUtils.isEmpty(info.getErrorMessage())){
                        ToastUtil.showMessage(info.getErrorMessage());
                    }
                    softConfigInfo = info;
                    onLoadSuccess(context, softConfigInfo);
                    Log4an.e(logTag, "请求开启软件数据:联网请求完成：");
                } else {
                    Log4an.e(logTag, "请求开启软件数据:请求失败");
                }
            }
        };
        GetSoftConfigOnlineModel model =
                new GetSoftConfigOnlineModel(context.getApplicationContext(), listener);
        model.loadData();
        Log4an.e(logTag, "请求开启软件数据:联网请求中..");
    }

    /** 加载完成后保存处理数据 */
    private void onLoadSuccess(Context context, SoftConfigInfo info) {
        Log4an.e(logTag, "加载完成后，开始保存数据...");
        if (info != null) {
            Editor editor =
                    context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_CONFIG,
                            Context.MODE_PRIVATE).edit();
            // 保存推广墙开关
            editor.putBoolean("offerWall", info.isOpenOfferWall());
            // 保存cdn阅读开关
            editor.putBoolean("cdnRead", info.isOpenCDNRead());
            // 保存统计开关
            editor.putInt("PvLimit", info.getPvLimit());
            editor.commit();
            // 详细统计暂未用到
            // 启动图
            if (!TextUtils.isEmpty(info.getLoadingImgUrl())) {
                LoadingImg.downloadLoadingPic(context, info.getLoadingImgUrl(),SharedPreferenceUtil.PicPathKey,Config.DEFAULT_PATH_LOADINGPIC);
                Log4an.e(logTag, "启动图开始下载..");
            }
            // 服务器备选重试域名
            ServerDomains.getInstance().saveServerList(info.getGroupList(), context);

            // 书旗网页域名
            if (!TextUtils.isEmpty(info.getWebDomains())) {
                WebDomains.getInstence().saveShuqiWebDmains(context, info.getWebDomains().toString());
            }

            // 书旗网各个地址
            WebUrls.getInstence().saveWebUrls(info.getWebUrls(), context);
            
            //保存静态域名
            StaticDomains.getInstance().saveStaticDomains(info.getStaticDomain(), context);

            saveOfferWallList(info.getOfferWallAppInfos(), context);
            Log4an.e(logTag, "加载完成后，所有数据保存完成");
        } else {
            Log4an.e(logTag, "加载完成后，开始保存数据...error info is null");
        }
    }

    /**
     * 推广墙列表
     * 
     * @param offerWallAppInfos
     * @param context
     */
    private void saveOfferWallList(List<OfferWallAppInfo> offerWallAppInfos, Context context) {

        if (offerWallAppInfos != null && offerWallAppInfos.size() > 0) {
            SharedPreferences sp =
                    context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_OFFERWALL,
                            Context.MODE_PRIVATE);
            Editor editor = sp.edit();
            editor.putInt("offerwallCount", offerWallAppInfos.size());
            for (int i = 0; i < offerWallAppInfos.size(); i++) {
                editor.putString("id_" + i, offerWallAppInfos.get(i).getId());
                editor.putString("name_" + i, offerWallAppInfos.get(i).getPackagename());
                editor.putString("url_" + i, offerWallAppInfos.get(i).getImgurl());
            }
            editor.commit();
        }
    }

}
